<?php

/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoathis_Convertor
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoathis_Convertor_Exception
 */
importModule('Convertor.Exception');

/**
 * Class Hoathis_Convertor_Format_Abstract
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Convertor
 * @subpackage  Hoathis_Convertor_Format
 */
abstract class Hoathis_Convertor_Format_Abstract {

    /**
     * Value being converted
     */
    public $value = null;

    /**
     * Converted value
     */
    public $_value = null;

    /**
     * Unit being converted
     */
    public $from = null;

    /**
     * Converted unit
     */
    public $to = null;

    /**
     * Filter array
     */
    protected $filters = array();

    /**
     * Units array
     */
    protected $units = array();

    /**
     * Constructor
     *
     * @param	string	value	Value being converted
     * @param	string 	from    Current unit
     * @param	string 	to		Convertor unit
     */
    public function __construct( $value = null, $from = null, $to = null ) {

        if (is_null($value) OR empty($value))
            throw new Hoathis_Convertor_Exception('Argument 1 value must be a not empty string', 2);

        if (is_null($from) OR !is_string($from))
            throw new Hoathis_Convertor_Exception('Argument 1 value must be a not empty string', 2);

        if (is_null($to))
            $to = $from;
        else if (!is_string($to))
            throw new Hoathis_Convertor_Exception('Argument 1 value must be a not empty string', 2);

        $value = trim($value);
        $from = trim($from);
        $to = trim($to);

        if (!self::unit_exist($from))
            throw new Hoathis_Convertor_Exception('From unit not exist', 2);

        if (!self::unit_exist($to))
            throw new Hoathis_Convertor_Exception('To unit not exist', 2);

        $this->_value = $this->convert($value, $from, $to);
    }

    /**
     * convert
     *
     * @param	string	value	Value being converted
     * @param	string 	from    Current unit
     * @param	string 	to		Convertor unit
     * @return	mixte			Converted value
     */
    abstract protected function convert( $value, $from, $to);

    /**
     * __tostring
     *
     * @return	string	Formatted value
     */
    public function __tostring() {

        return (string) $this->_value;
    }

    /**
     * addFilter
     *
     * @param	string	filter	filter to test if exist
     * @return	boolean	Return TRUE if the filter exist
     */
    public function addFilter( $filter = null, $param = array() ) {

        if (is_null($filter) OR !is_array($param))
            return;

        if (!self::filter_exist($filter))
            throw new Hoathis_Convertor_Exception('Filtre '.$filter.' introuvable', 2);

        $classname = 'Hoathis_Convertor_Filter_'.ucfirst(strtolower($filter));

        return $this->_value = new $classname($this->_value, $param);
    }

    /**
     * addFilters
     *
     * @param	string	filter	Filter to test if exist
     * @return	boolean			Return TRUE if the filter exist
     */
    public function addFilters( $filters = array() ) {

        if (!is_array($filters))
            return;

        foreach($filters AS $filter => $param)
            $this->addFilter($filter, $param);

        return $this->_value;
    }

    /**
     * unit_exist
     *
     * @param	string	unit	Unit to test
     * @return	boolean			Return TRUE if the unit exist
     */
    private function unit_exist( $unit = null ) {

        if (is_null($unit) AND !is_string($unit))
            throw new Hoathis_Convertor_Exception('the unit "'.$unit.'" is invalid', 2);

        return array_key_exists($unit, $this->units);
    }

    /**
     * filter_exist
     *
     * @return	string	unit	Filter to test
     * @return	boolean	Return TRUE if the filter exist
     */
    private function filter_exist( $filter = null ) {

        return in_array($filter, $this->filters);
    }
}